/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 4.5.2011, 1:02:10
 */

package src.clovece;

import java.awt.Point;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import src.clovece.game.AiPlayer;
import src.clovece.game.GameInfo;

/**
 * Class serving as menu with computer player preferences
 */
public class AiPreferencesDialog extends javax.swing.JDialog {
    private GameFrame parent;
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();

    /** Creates new form AiPreferencesDialog */
    public AiPreferencesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        Point p = parent.getLocation();
        this.parent = (GameFrame) parent;
        this.setLocation(p.x +250, p.y +250);
        initComponents();
    }

    /*
     * Initialises and shows dialog
     */
    public void showYourself() {
        ButtonGroup group = new ButtonGroup();
        AiPlayer computer = null;
        group.add(this.ManualRadioButton);
        group.add(this.AutomaticRadioButton);
        if(!this.gameInfo.getHasStarted()) {
            /*
             * disabling preferences for unused computer players
             */
            this.AutomaticRadioButton.setEnabled(false);
            this.ManualRadioButton.setEnabled(false);
            this.ComputerComboBox1.setEnabled(false);
            this.ComputerComboBox2.setEnabled(false);
            this.ComputerComboBox3.setEnabled(false);
            this.PreferHomeCheckBox1.setEnabled(false);
            this.PreferHomeCheckBox2.setEnabled(false);
            this.PreferHomeCheckBox3.setEnabled(false);
            this.PreferNewCheckBox1.setEnabled(false);
            this.PreferNewCheckBox2.setEnabled(false);
            this.PreferNewCheckBox3.setEnabled(false);
            this.SaveButton.setEnabled(false);
        }
        else {
            if(this.gameInfo.getNumberOfPlayers() == 2) {
                /*
                 * setting user preferences for computers
                 */
                /// computer 1
                computer = (AiPlayer) this.gameInfo.getPlayers()[1];
                if(computer.getRandomMove()) this.ComputerComboBox1.setSelectedIndex(0);
                else this.ComputerComboBox1.setSelectedIndex(1);
                if(computer.getPreferHome()) this.PreferHomeCheckBox1.setSelected(true);
                else this.PreferHomeCheckBox1.setSelected(false);
                if(computer.getPreferNew ()) this.PreferNewCheckBox1.setSelected(true);
                else this.PreferNewCheckBox1.setSelected(false);
                /*
                 * disabling preferences for unused computer players
                 */
                this.ComputerComboBox2.setEnabled(false);
                this.ComputerComboBox3.setEnabled(false);
                this.PreferHomeCheckBox2.setEnabled(false);
                this.PreferHomeCheckBox3.setEnabled(false);
                this.PreferNewCheckBox2.setEnabled(false);
                this.PreferNewCheckBox3.setEnabled(false);
            }
            else if(this.gameInfo.getNumberOfPlayers() == 3) {
                /*
                 * setting user preferences for computers
                 */
                /// computer 1
                computer = (AiPlayer) this.gameInfo.getPlayers()[1];
                if(computer.getRandomMove()) this.ComputerComboBox1.setSelectedIndex(0);
                else this.ComputerComboBox1.setSelectedIndex(1);
                if(computer.getPreferHome()) this.PreferHomeCheckBox1.setSelected(true);
                else this.PreferHomeCheckBox1.setSelected(false);
                if(computer.getPreferNew ()) this.PreferNewCheckBox1.setSelected(true);
                else this.PreferNewCheckBox1.setSelected(false);
                /// computer 2
                computer = (AiPlayer) this.gameInfo.getPlayers()[2];
                if(computer.getRandomMove()) this.ComputerComboBox2.setSelectedIndex(0);
                else this.ComputerComboBox2.setSelectedIndex(1);
                if(computer.getPreferHome()) this.PreferHomeCheckBox2.setSelected(true);
                else this.PreferHomeCheckBox2.setSelected(false);
                if(computer.getPreferNew ()) this.PreferNewCheckBox2.setSelected(true);
                else this.PreferNewCheckBox2.setSelected(false);
                /*
                 * disabling preferences for unused computer players
                 */
                this.ComputerComboBox3.setEnabled(false);
                this.PreferHomeCheckBox3.setEnabled(false);
                this.PreferNewCheckBox3.setEnabled(false);
            }
            else {
                /*
                 * setting user preferences for computers
                 */
                /// computer 1
                computer = (AiPlayer) this.gameInfo.getPlayers()[1];
                if(computer.getRandomMove()) this.ComputerComboBox1.setSelectedIndex(0);
                else this.ComputerComboBox1.setSelectedIndex(1);
                if(computer.getPreferHome()) this.PreferHomeCheckBox1.setSelected(true);
                else this.PreferHomeCheckBox1.setSelected(false);
                if(computer.getPreferNew ()) this.PreferNewCheckBox1.setSelected(true);
                else this.PreferNewCheckBox1.setSelected(false);
                /// computer 2
                computer = (AiPlayer) this.gameInfo.getPlayers()[2];
                if(computer.getRandomMove()) this.ComputerComboBox2.setSelectedIndex(0);
                else this.ComputerComboBox2.setSelectedIndex(1);
                if(computer.getPreferHome()) this.PreferHomeCheckBox2.setSelected(true);
                else this.PreferHomeCheckBox2.setSelected(false);
                if(computer.getPreferNew ()) this.PreferNewCheckBox2.setSelected(true);
                else this.PreferNewCheckBox2.setSelected(false);
                /// computer 3
                computer = (AiPlayer) this.gameInfo.getPlayers()[3];
                if(computer.getRandomMove()) this.ComputerComboBox3.setSelectedIndex(0);
                else this.ComputerComboBox3.setSelectedIndex(1);
                if(computer.getPreferHome()) this.PreferHomeCheckBox3.setSelected(true);
                else this.PreferHomeCheckBox3.setSelected(false);
                if(computer.getPreferNew ()) this.PreferNewCheckBox3.setSelected(true);
                else this.PreferNewCheckBox3.setSelected(false);
            }
            
        }
        this.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Computer1Panel = new javax.swing.JPanel();
        PreferHomeCheckBox1 = new javax.swing.JCheckBox();
        PreferNewCheckBox1 = new javax.swing.JCheckBox();
        ComputerComboBox1 = new javax.swing.JComboBox();
        SaveButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        Computer2Panel = new javax.swing.JPanel();
        PreferHomeCheckBox2 = new javax.swing.JCheckBox();
        PreferNewCheckBox2 = new javax.swing.JCheckBox();
        ComputerComboBox2 = new javax.swing.JComboBox();
        Computer3Panel = new javax.swing.JPanel();
        PreferHomeCheckBox3 = new javax.swing.JCheckBox();
        PreferNewCheckBox3 = new javax.swing.JCheckBox();
        ComputerComboBox3 = new javax.swing.JComboBox();
        AutomaticRadioButton = new javax.swing.JRadioButton();
        ManualRadioButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        Computer1Panel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Computer 1", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        PreferHomeCheckBox1.setText("prefer home");

        PreferNewCheckBox1.setText("prefer new");

        ComputerComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Random", "Longest jump" }));

        javax.swing.GroupLayout Computer1PanelLayout = new javax.swing.GroupLayout(Computer1Panel);
        Computer1Panel.setLayout(Computer1PanelLayout);
        Computer1PanelLayout.setHorizontalGroup(
            Computer1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Computer1PanelLayout.createSequentialGroup()
                .addGroup(Computer1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(Computer1PanelLayout.createSequentialGroup()
                        .addComponent(PreferHomeCheckBox1)
                        .addGap(18, 18, 18)
                        .addComponent(PreferNewCheckBox1))
                    .addGroup(Computer1PanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ComputerComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        Computer1PanelLayout.setVerticalGroup(
            Computer1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Computer1PanelLayout.createSequentialGroup()
                .addGroup(Computer1PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PreferHomeCheckBox1)
                    .addComponent(PreferNewCheckBox1))
                .addGap(18, 18, 18)
                .addComponent(ComputerComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        SaveButton.setText("Save");
        SaveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SaveButtonMouseClicked(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });

        Computer2Panel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Computer 2", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        PreferHomeCheckBox2.setText("prefer home");

        PreferNewCheckBox2.setText("prefer new");

        ComputerComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Random", "Longest jump" }));

        javax.swing.GroupLayout Computer2PanelLayout = new javax.swing.GroupLayout(Computer2Panel);
        Computer2Panel.setLayout(Computer2PanelLayout);
        Computer2PanelLayout.setHorizontalGroup(
            Computer2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Computer2PanelLayout.createSequentialGroup()
                .addGroup(Computer2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(Computer2PanelLayout.createSequentialGroup()
                        .addComponent(PreferHomeCheckBox2)
                        .addGap(18, 18, 18)
                        .addComponent(PreferNewCheckBox2))
                    .addGroup(Computer2PanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ComputerComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        Computer2PanelLayout.setVerticalGroup(
            Computer2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Computer2PanelLayout.createSequentialGroup()
                .addGroup(Computer2PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PreferHomeCheckBox2)
                    .addComponent(PreferNewCheckBox2))
                .addGap(18, 18, 18)
                .addComponent(ComputerComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        Computer3Panel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Computer 3", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        PreferHomeCheckBox3.setText("prefer home");

        PreferNewCheckBox3.setText("prefer new");

        ComputerComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Random", "Longest jump" }));

        javax.swing.GroupLayout Computer3PanelLayout = new javax.swing.GroupLayout(Computer3Panel);
        Computer3Panel.setLayout(Computer3PanelLayout);
        Computer3PanelLayout.setHorizontalGroup(
            Computer3PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Computer3PanelLayout.createSequentialGroup()
                .addGroup(Computer3PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(Computer3PanelLayout.createSequentialGroup()
                        .addComponent(PreferHomeCheckBox3)
                        .addGap(18, 18, 18)
                        .addComponent(PreferNewCheckBox3))
                    .addGroup(Computer3PanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ComputerComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        Computer3PanelLayout.setVerticalGroup(
            Computer3PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(Computer3PanelLayout.createSequentialGroup()
                .addGroup(Computer3PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PreferHomeCheckBox3)
                    .addComponent(PreferNewCheckBox3))
                .addGap(18, 18, 18)
                .addComponent(ComputerComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        AutomaticRadioButton.setMnemonic(KeyEvent.VK_D);
        AutomaticRadioButton.setSelected(true);
        AutomaticRadioButton.setText("Automatic ");

        ManualRadioButton.setMnemonic(KeyEvent.VK_D);
        ManualRadioButton.setText("Manual");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Computer3Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ManualRadioButton)
                            .addComponent(AutomaticRadioButton))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(SaveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Computer1Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(Computer2Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Computer1Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Computer2Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(Computer3Panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(SaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(AutomaticRadioButton)
                                .addGap(28, 28, 28)
                                .addComponent(ManualRadioButton)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SaveButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SaveButtonMouseClicked
        AiPlayer computer = null;
        if(!this.SaveButton.isEnabled()) return;
        for(int i = 1; i < this.gameInfo.getNumberOfPlayers(); i++) {
            computer = (AiPlayer) this.gameInfo.getPlayers()[i];
            if(i == 1) {
                if(this.PreferHomeCheckBox1.isSelected()) {
                    computer.setPreferHome(true);
                }
                else {
                    computer.setPreferHome(false);
                }
                if(this.PreferNewCheckBox1.isSelected()) {
                    computer.setPreferNew(true);
                }
                else {
                    computer.setPreferNew(false);
                }
                if(this.ComputerComboBox1.getSelectedItem().toString().equals("Random")) {
                    computer.setRandomMove(true);
                }
                else {
                    computer.setRandomMove(false);
                }
            }
            else if(i == 2) {
                if(this.PreferHomeCheckBox2.isSelected()) {
                    computer.setPreferHome(true);
                }
                else {
                    computer.setPreferHome(false);
                }
                if(this.PreferNewCheckBox2.isSelected()) {
                    computer.setPreferNew(true);
                }
                else {
                    computer.setPreferNew(false);
                }
                if(this.ComputerComboBox2.getSelectedItem().toString().equals("Random")) {
                    computer.setRandomMove(true);
                }
                else {
                    computer.setRandomMove(false);
                }
            }
            else if(i == 3) {
                if(this.PreferHomeCheckBox3.isSelected()) {
                    computer.setPreferHome(true);
                }
                else {
                    computer.setPreferHome(false);
                }
                if(this.PreferNewCheckBox3.isSelected()) {
                    computer.setPreferNew(true);
                }
                else {
                    computer.setPreferNew(false);
                }
                if(this.ComputerComboBox3.getSelectedItem().toString().equals("Random")) {
                    computer.setRandomMove(true);
                }
                else {
                    computer.setRandomMove(false);
                }
            }
        }
        if(this.AutomaticRadioButton.isSelected()) {
            System.out.println("automatic");
        }
        else {
            System.out.println("manual");
        }
        this.dispose();
    }//GEN-LAST:event_SaveButtonMouseClicked

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        this.dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton AutomaticRadioButton;
    private javax.swing.JButton CancelButton;
    private javax.swing.JPanel Computer1Panel;
    private javax.swing.JPanel Computer2Panel;
    private javax.swing.JPanel Computer3Panel;
    private javax.swing.JComboBox ComputerComboBox1;
    private javax.swing.JComboBox ComputerComboBox2;
    private javax.swing.JComboBox ComputerComboBox3;
    private javax.swing.JRadioButton ManualRadioButton;
    private javax.swing.JCheckBox PreferHomeCheckBox1;
    private javax.swing.JCheckBox PreferHomeCheckBox2;
    private javax.swing.JCheckBox PreferHomeCheckBox3;
    private javax.swing.JCheckBox PreferNewCheckBox1;
    private javax.swing.JCheckBox PreferNewCheckBox2;
    private javax.swing.JCheckBox PreferNewCheckBox3;
    private javax.swing.JButton SaveButton;
    // End of variables declaration//GEN-END:variables

}
